package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.mapper.SmsHomeRecommendProductMapper;
import com.macro.mall.mapper.UmsStoreMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.SmsHomeRecommendProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页人气推荐管理Service实现类
 * Created by macro on 2018/11/7.
 */
@Service
public class SmsHomeRecommendProductServiceImpl implements SmsHomeRecommendProductService {
    @Autowired
    private SmsHomeRecommendProductMapper recommendProductMapper;
    @Autowired
    private UmsStoreMapper umsStoreMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Override
    public int create(Long storeId,Long memberId,String memberUserName, List<SmsHomeRecommendProduct> homeRecommendProductList) {
        for (SmsHomeRecommendProduct recommendProduct : homeRecommendProductList) {
            SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
            example.createCriteria().andProductIdEqualTo(recommendProduct.getProductId());
            List<SmsHomeRecommendProduct> smsHomeRecommendProducts = recommendProductMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(smsHomeRecommendProducts)) {
                return -2;
            }
            recommendProduct.setRecommendStatus(1);
            recommendProduct.setSort(0);
            recommendProduct.setStoreId(storeId);
            recommendProductMapper.insert(recommendProduct);
            //把商品的推荐状态改了
            PmsProduct product = productMapper.selectByPrimaryKey(recommendProduct.getProductId());
            product.setRecommandStatus(recommendProduct.getRecommendStatus());
            productMapper.updateByPrimaryKeySelective(product);
        }
        return homeRecommendProductList.size();
    }

    @Override
    public int updateSort(Long storeId,Long memberId,String memberUserName, Long id, Integer sort) {
        SmsHomeRecommendProduct recommendProduct = new SmsHomeRecommendProduct();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        return recommendProductMapper.updateByPrimaryKeySelective(recommendProduct);
    }

    @Override
    public int delete(Long storeId,Long memberId,String memberUserName, List<Long> ids) {
        SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
        example.createCriteria().andIdIn(ids);
        //把商品的推荐状态改了
        List<SmsHomeRecommendProduct> smsHomeRecommendProducts = recommendProductMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(smsHomeRecommendProducts)) {
            for (SmsHomeRecommendProduct sms : smsHomeRecommendProducts) {
                PmsProduct product = productMapper.selectByPrimaryKey(sms.getProductId());
//                product.setRecommandStatus(sms.getRecommendStatus());
                product.setRecommandStatus(0);
                productMapper.updateByPrimaryKeySelective(product);
            }
        }
        int i = recommendProductMapper.deleteByExample(example);
        return i;
    }

    @Override
    public int updateRecommendStatus(Long storeId,Long memberId,String memberUserName, List<Long> ids, Integer recommendStatus) {
        SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
        example.createCriteria().andIdIn(ids);
        SmsHomeRecommendProduct record = new SmsHomeRecommendProduct();
        record.setRecommendStatus(recommendStatus);
        //更新商品的推荐状态
        List<Long> productIds = new ArrayList<>();
        List<SmsHomeRecommendProduct> list = recommendProductMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            for (SmsHomeRecommendProduct sms : list) {
                PmsProduct product = productMapper.selectByPrimaryKey(sms.getProductId());
                productIds.add(product.getId());
            }
        }
        PmsProduct pro = new PmsProduct();
        pro.setRecommandStatus(recommendStatus);
        PmsProductExample productExample = new PmsProductExample();
        productExample.createCriteria().andIdIn(productIds);
        productMapper.updateByExampleSelective(pro, productExample);
        return recommendProductMapper.updateByExampleSelective(record,example);
    }

    @Override
    public List<SmsHomeRecommendProduct> list(UmsAdminDTO umsAdminDTO, Long memberId, String memberUserName, String productName,
                                              Integer recommendStatus, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
        SmsHomeRecommendProductExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(productName)){
            criteria.andProductNameLike("%"+productName+"%");
        }
        if(recommendStatus!=null){
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        if(umsAdminDTO.getAdminType().equals(2)){
            criteria.andStoreIdEqualTo(umsAdminDTO.getStoreId());
        }
        if(umsAdminDTO.getAdminType().equals(1)){
            UmsStoreExample storeExample = new UmsStoreExample();
            storeExample.createCriteria().andParentIdEqualTo(umsAdminDTO.getId());
            List<Long> stoIds = this.umsStoreMapper.selectByExample(storeExample).stream().map(UmsStore::getId).collect(Collectors.toList());
            criteria.andStoreIdIn(stoIds);
        }
        example.setOrderByClause("sort desc");
        return recommendProductMapper.selectByExample(example);
    }
}
